package com.rub.android.jetpack.components

import android.content.Context
import android.os.SystemClock
import android.util.AttributeSet
import android.widget.Chronometer
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent

class MyChronometer @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null
) : Chronometer(context, attrs), LifecycleObserver {

    private var elapsedTime: Long = 0

    @OnLifecycleEvent(Lifecycle.Event.ON_RESUME)
    private fun startMeter() {
        base = SystemClock.elapsedRealtime() - elapsedTime
        this.start()
    }
    @OnLifecycleEvent(Lifecycle.Event.ON_PAUSE)
    private fun stopMeter() {
        elapsedTime = SystemClock.elapsedRealtime() - base
        this.stop()
    }
}